<?php
    $db = new SQLiteDatabase('InstantInput.db');
    
    $result = $db->query("SELECT sunet_id FROM USERS WHERE sunet_id = '" . $_ENV["WEBAUTH_USER"] . "'");
	if ($result->numRows() == 0) 
	{
		$db->queryExec("INSERT INTO Users (sunet_id, type, email) VALUES ('" . $_ENV["WEBAUTH_USER"] . "', '" . $_ENV["WEBAUTH_LDAPAUTHRULE"] . "', '" . $_ENV["WEBAUTH_LDAP_MAIL"] . "')");
	}
	
	$classes = array();

	$result = $db->query("SELECT * FROM Classes WHERE Classes.class_id IN (SELECT User_Classes.class_id FROM User_Classes WHERE User_Classes.sunet_id = '" . $_ENV["WEBAUTH_USER"] . "')");
	
	while ($result->valid()) 
	{
		$curr = $result->current();
	
		$class = array(
			"uniqueCourseNumber"=> $curr['class_id'],
			"course"=>$curr['course'],
			"title"=>$curr['title'],
			"startTime"=>$curr['start_time'],
			"endTime"=>$curr['end_time'],
			"days"=>array("F"),
			"location"=>$curr['location']
		);
		
		$classes[] = $class;
		$result->next();	
	}	
	
	print json_encode($classes);

?>